package main

import (
	"fmt"
	"github.com/go-playground/locales/zh"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	vtzh "github.com/go-playground/validator/v10/translations/zh"
)

type People struct {
	FirstName string `validate:"required"`
	LastName  string `validate:"required"`
	Age       uint8  `validate:"gte=0,lte=130"`
	Email     string `validate:"required,email"`
}

func main() {
	p := &People{
		FirstName: "Jafferson",
		LastName:  "Clinton",
		Age:       132,
		Email:     "123163.com",
	}
	validate := validator.New(validator.WithRequiredStructEnabled())
	// 消息自定义&国际化
	// 创建消息国际化通用翻译器
	cn := zh.New()
	uni := ut.New(cn, cn)
	translator, found := uni.GetTranslator("zh")
	if found {
		err := vtzh.RegisterDefaultTranslations(validate, translator)
		if err != nil {
			fmt.Println(err)
		}
	} else {
		fmt.Println("not fount")
	}
	err := validate.Struct(p)
	if err != nil {
		validationErrors := err.(validator.ValidationErrors)

		for _, validationError := range validationErrors {
			fmt.Println(validationError.Translate(translator))
		}
	}

}
